package com.hjm.project.forceReplace.forceReplace.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.common.utils.FileOrImgUtils;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.config.BlueConfig;
import com.hjm.framework.config.CustomConfig;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.project.agentInfo.agentInfo.utils.Utils;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.project.forceReplace.forceReplace.mapper.ForceReplaceMapper;
import com.hjm.project.forceReplace.forceReplace.domain.ForceReplace;
import com.hjm.project.forceReplace.forceReplace.service.IForceReplaceService;
import com.hjm.common.utils.text.Convert;
import org.springframework.web.multipart.MultipartFile;

/**
 * 更新操作Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-26
 */
@Service
public class ForceReplaceServiceImpl implements IForceReplaceService 
{
    @Autowired
    private ForceReplaceMapper forceReplaceMapper;

    /**
     * 查询更新操作
     * 
     * @param id 更新操作ID
     * @return 更新操作
     */
    @Override
    public ForceReplace selectForceReplaceById(Long id)
    {
        return forceReplaceMapper.selectForceReplaceById(id);
    }

    /**
     * 查询更新操作列表
     * 
     * @param forceReplace 更新操作
     * @return 更新操作
     */
    @Override
    public List<ForceReplace> selectForceReplaceList(ForceReplace forceReplace)
    {
        return forceReplaceMapper.selectForceReplaceList(forceReplace);
    }

    /**
     * 新增更新操作
     * 
     * @param forceReplace 更新操作
     * @return 结果
     */
    @Override
    public int insertForceReplace(ForceReplace forceReplace)
    {
        return forceReplaceMapper.insertForceReplace(forceReplace);
    }

    /**
     * 修改更新操作
     * 
     * @param forceReplace 更新操作
     * @return 结果
     */
    @Override
    public int updateForceReplace(ForceReplace forceReplace)
    {
        forceReplace.setUpdatetime(DateUtils.getDateTime());
        return forceReplaceMapper.updateForceReplace(forceReplace);
    }

    @Override
    public AjaxResult saveApkPackage(MultipartFile apkFile, ForceReplace forceReplace)throws Exception {
        try {
            if (StringUtils.isBlank(apkFile.getOriginalFilename())) {
                forceReplace.setNote("安装包不可为空");
                forceReplaceMapper.updateForceReplace(forceReplace);
                return AjaxResult.error("安装包不可为空");
            }
            if (!apkFile.getOriginalFilename().endsWith(".apk")) {
                forceReplace.setNote("请上传正确的apk安装包");
                forceReplaceMapper.updateForceReplace(forceReplace);
                return AjaxResult.error("请上传正确的apk安装包");
            }
            forceReplace.setNote("正在上传中,请稍等。。。。");
            forceReplaceMapper.updateForceReplace(forceReplace);
            //上传本地
            //String path = FileUploadUtils.upload1(BlueConfig.getAvatarPath(), apkFile);

            ForceReplace forceReplace1 = forceReplaceMapper.selectForceReplaceById(Long.parseLong(String.valueOf(forceReplace.getId())));
            //上传文件
            String address = forceReplace1.getAddress();
            String fileId = null;
            if (StringUtils.isNotBlank(address)) {
                int index = address.lastIndexOf("/group1") + 1;
                if (index > -1) {
                    fileId = address.substring(index,address.length());
                }
            }
            String urlName = FileOrImgUtils.uploadImgApk(apkFile);
            forceReplace.setNote("上传成功");
            forceReplace.setAddress(CustomConfig.getImgUrl() + urlName);
            forceReplace.setUpdatetime(DateUtils.getDateTime());
            forceReplace.setFileName(apkFile.getOriginalFilename());
            forceReplaceMapper.updateForceReplace(forceReplace);
        } catch (Exception e) {
            forceReplace.setNote("上传异常");
            forceReplaceMapper.updateForceReplace(forceReplace);
            return AjaxResult.error("上传异常");
        }
        return AjaxResult.success();
    }

    /**
     * 删除更新操作对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteForceReplaceByIds(String ids)
    {
        return forceReplaceMapper.deleteForceReplaceByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除更新操作信息
     * 
     * @param id 更新操作ID
     * @return 结果
     */
    @Override
    public int deleteForceReplaceById(Long id)
    {
        return forceReplaceMapper.deleteForceReplaceById(id);
    }

    public String uploadImg(MultipartFile imgFile,String fileId) throws IOException {
        String clientName = imgFile.getOriginalFilename();
        if (StringUtils.isEmpty(clientName)) {
            return null;
        }
        if (StringUtils.isNotBlank(fileId)) {
            FastDFSClient.deleteFile(fileId);
        }
        //获取一个要上传的路径
        String downloadPath = BlueConfig.getDownloadPath();
        //创建文件目录
        File files = new File(downloadPath);
        if (!files.exists()) {
            files.mkdirs();
        }
        File file = new File(downloadPath + File.separator + imgFile.getOriginalFilename());
        //imgFile.transferTo(file);
        FileUtils.copyInputStreamToFile(imgFile.getInputStream(),file);
        //上传至Fast的图片数据
        Map<String, String> metaList = new HashMap<String, String>();
        metaList.put("fileName", imgFile.getName());
        metaList.put("fileType", imgFile.getName());
        metaList.put("author", ShiroUtils.getSysUser().getLoginName());
        metaList.put("date", Utils.formateDate(2));//年月日 时分秒
        String fid = FastDFSClient.uploadFile(file, file.getName(), metaList);
        if (StringUtils.isEmpty(fid)) {
            throw  new BusinessException("图片上传失败,请重新上传");
        }
        file.delete();
        return fid;
    }

}
